using System;
using System.Collections.Generic;
using System.Data;

using System.IO;
using Lizk.SimpleHUD;
using Lizk.WebScraper;
using SharpMedia.Controls;
using System.Linq;
using System.Threading;

namespace SharpMedia.Pages
{
    internal class EpisodeSelectionPage2 : MediaPage
    {
        private TestData.TitlesRow title;
        private ScraperResult scrapedInfo;

        private HUDLabel show, genres, summary;
        private HUDContainerSimple imageContainer = new HUDContainerSimple();
        private HUDImage titleImage;
        private ScraperIcon scraperImage;

        private HUDTwistList twistList;

        public EpisodeSelectionPage2(MediaPage parent, TestData.TitlesRow title, RectangleS imagePos, SizeS imageSize)
            : base(parent)
        {
            this.title = title;
            InitializeComponents(imagePos, imageSize, null);
        }

        public EpisodeSelectionPage2(MediaPage parent, TestData.TitlesRow title, RectangleS imagePos, SizeS imageSize, TestData.EpisodesRow selectedEpisode)
            : base(parent)
        {
            this.title = title;
            InitializeComponents(imagePos, imageSize, selectedEpisode);
        }


        void image_ImageSizeChanged(object sender, EventArgs e)
        {
            RectangleS s = HUDImage.ResizeConstrained(true, titleImage.ImageSize.Size, titleImage.Size);
            scraperImage.Bounds = new RectangleS(titleImage.Bounds.Left + s.Right - 42, titleImage.Bounds.Top + s.Bottom - 42, 42, 42);
        }

        private void InitializeComponents(RectangleS imagePos, SizeS imageSize, TestData.EpisodesRow selectedEpisode)
        {
            if (imageSize == SizeS.Empty) imageSize = new SizeS(1, 1);

            string image = SharpMedia.ImageCache.TryGetImagePath(title, "film.png");
            

            imageContainer.Bounds = imagePos;
            imageContainer.Destination = new PointS(250, 10);
            imageContainer.DestinationSize = new PointS(160f / imageSize.Height * imageSize.Width, 160f);
            imageContainer.MovementStopped += new EventHandler(delegate(object o, EventArgs ea)
                                                           {
                                                               show.Fade(255, 6f);
                                                               if(genres != null)
                                                                genres.Fade(255, 6f);
                                                               summary.Fade(255, 6f);
                                                           
                                                               RectangleS s = HUDImage.ResizeConstrained(true, titleImage.ImageSize.Size, titleImage.Size);
                                                               scraperImage.Bounds = new RectangleS(titleImage.Bounds.Left + s.Right - 42, titleImage.Bounds.Top + s.Bottom - 42, 42, 42);
                                                           });
            imageContainer.MovementSpeed = 8f;
            imageContainer.ScaleSpeed = 8f;
            Add(imageContainer);


            titleImage = new HUDImage(image);
            titleImage.Size = imageContainer.Size;
            titleImage.Anchor = Anchor.All;
            titleImage.ImageSizeChanged +=new EventHandler(image_ImageSizeChanged);

            imageContainer.Add(titleImage);


            scraperImage = new ScraperIcon(title);
            scraperImage.Bounds = new RectangleS(imageContainer.Bounds.Right - 40, imageContainer.Bounds.Bottom - 40, 32, 32);
            scraperImage.Alpha = 0;
            imageContainer.Add(scraperImage);

            show = new HUDLabel(title.Name, "tahoma38b");
            show.Bounds = new RectangleS(imageContainer.Destination.X + imageContainer.DestinationSize.X + 15, imageContainer.Destination.Y + 5, Bounds.Width - imageContainer.Destination.X - imageContainer.DestinationSize.X - 15, 40);
            show.Alpha = 0;
            Add(show);

            summary = new HUDLabel(title.Description, "tahoma14b");
            summary.Anchor = Anchor.All ^ Anchor.Bottom;
            summary.Alpha = 0;
            Add(summary);


            scrapedInfo = SharpMedia.ScraperCache.GetResult(title);


            if (scrapedInfo != null && scrapedInfo.Genres.Count > 0)
            {
                genres = new HUDLabel(String.Join(", ", scrapedInfo.Genres.ToArray()), "tahoma20b");
                genres.Bounds = new RectangleS(show.Bounds.Left + 5, show.Bounds.Bottom - 5, show.Bounds.Width - 5, 25);
                genres.Alpha = 0;
                Add(genres);
                summary.Text = scrapedInfo.Summary;
                summary.Bounds = new RectangleS(show.Bounds.Left, genres.Bounds.Bottom + 1, show.Bounds.Width - 5, imageContainer.Destination.Y + imageContainer.DestinationSize.Y - genres.Bounds.Bottom - 1);
            }
            else
            {
                summary.Bounds = new RectangleS(show.Bounds.Left, show.Bounds.Bottom - 5, show.Bounds.Width - 5, 125);
            }

            twistList = new HUDTwistList()
            {
                Location = new PointS(0, 170f),
                LeftMargin = 50,
                RightMargin = 50,
                Padding = 10,
                Size = new SizeS(Size.Width, 50),
                Anchor = Anchor.All ^ Anchor.Bottom,
                Offset = new PointS(100, 0)
            };
            Add(twistList);

            for (int i = 1; i < 7; i++)
            {
                HUDLabel lbl = new HUDLabel("Season " + i, "tahoma38b");
                lbl.Size = lbl.GetFittedSize(new SizeS(10000000, 1000000));
                twistList.Add(lbl);
            }
            twistList.MoveItems(false);



        }

        protected override void MediaChanged(TestData changes)
        {
            if (title.RowState == DataRowState.Deleted)
            {
                SharpMedia.Back();
                return;
            }

            
            
            if (genres != null && scrapedInfo != null && scrapedInfo.Genres.Count > 0)
                genres.Text = string.Join(", ", scrapedInfo.Genres.ToArray());
            
            
            
            summary.Text = title.Description;

            titleImage.File = SharpMedia.ImageCache.TryGetImagePath(title, "film.png");
        }


        public override void Command(HUDCommands command, object tag)
        {

            switch (command)
            {
                case HUDCommands.Left:
                    twistList.SelectedIndex--;
                    break;
                case HUDCommands.Right:
                    twistList.SelectedIndex++;
                    break;
                case HUDCommands.Back:

                    SharpMedia.Back();
                    break;
                case HUDCommands.Web:
                    if(title != null)
                        Util.TryScrape(this, title); 
                    break;
                default:
                    parent.Command(command, tag);
                    break;
            }
        }

        public override void Close()
        {
            Fade(0, 6f);
        }

        public override void Deactivate()
        {
        }

        public override void Show()
        {
            Fade(255, 6f);
        }

        public override void Activate()
        {
            Fade(255, 6f);
        }


    }
}